<?php
// +-------------------------------------------------------------+
// | Tinysupe [ SIMPLE IS THE BEST ! ]
// +-------------------------------------------------------------+
// | Copyright (c) 2011 www.srccn.com All rights reserved.
// +-------------------------------------------------------------+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +-------------------------------------------------------------+
// | Author: tinysu <tinysu@qq.com>
// +-------------------------------------------------------------+

class driver
{
	private $object = null;

	function __construct($durl) {
		defined('COREDIR') or die('Loader error!');
		
		$durl = parse_url($durl);
		$dfile = COREDIR.'/drivers/'.$durl['scheme'].'.php';
		if (file_exists($dfile)) {
			require_once($dfile);
			if (class_exists($durl['scheme'])) {
				$this->object = new $durl['scheme']($durl);
			} elseif (class_exists('drv_'.$durl['scheme'])) {
				$durl['scheme'] = 'drv_'.$durl['scheme'];
				$this->object = new $durl['scheme']($durl);
			} else {
				trigger_error('Could not find the '.$durl['scheme'].' class.', E_USER_ERROR);
			}
		} else {
			trigger_error('Could not find the '.$durl['scheme'].' driver.', E_USER_ERROR);
		}
	}

	function __destruct(){
		if($this->object) unset($this->object);
	}

	function __call($method, $args = array()) {
		$this->setModuleInformation();

		if (is_object($this->caller) && function_exists('call_user_func_array'))
			$return = call_user_func_array(array(&$this->caller, $funcname), $args);
		else
			trigger_error("Call to Function with call_user_func_array failed", E_USER_ERROR);
       
		$this->unsetModuleInformation();
		return $return;
	}

	function __set($set, $val) {
		$this->setModuleInformation();
		if (is_object($this->caller))
			$this->caller->{$set} = $val;
		else
			trigger_error('Caller is no object!', E_USER_ERROR);
		$this->unsetModuleInformation();
	}

	function __get($get) {
		$this->setModuleInformation();
		if (is_object($this->caller)) {
			if (isset($this->caller->{$get}))
				$return = $this->caller->{$get};
			else
				$return = false;
		}
		else
			trigger_error('Caller is no object!', E_USER_ERROR);
		$this->unsetModuleInformation();
		return $return;
	}
}
?>